// pages/my/addCustomGoods/addCustomGoods.js
import * as appConfig from '../../../app-config.js'
var app = getApp()
Page({
  data: {
    name: '',
    picUrl: '',
    detailPicUrls: ['../../../image/addPic.png'],
    isShow: true,
    sizes: [{
      name: "",
      infos: [""],
      lastId: 0
    }],
    success:false,
    agentId:''
  },
  onLoad: function(options) {
    app.getMemInfo().then(res=>{
      this.setData({
        agentId:res.data.agentId
      })
    })
    var sizes = [{
      name: "规格名称1",
      infos: ["1-1", "1-2"],
      lastId: 1

    }]
    this.setData({
      sizes: sizes
    })
  },
  //获取商品名称
  goodNameTap: function(event) {
    var value = event.detail.value;
    this.setData({
      name: value
    })
  },
  //删除规格分类内容
  descType: function(event) {
    var dataSet = event.currentTarget.dataset;
    var sizeIndex = dataSet.sizeIndex;
    var index = dataSet.index;
    var sizes = this.data.sizes;
    var nums = sizes[sizeIndex].infos.length;
    if (nums == 1) {
      sizes[sizeIndex].infos[index] = "";
    } else {
      var infos = sizes[sizeIndex].infos
      infos.splice(index, 1);
    }

    this.setData({
      sizes: sizes,
    })
    this.getLastId(sizeIndex)
  },
  addType: function(event) {
    var dataSet = event.currentTarget.dataset;
    var sizeIndex = dataSet.sizeIndex;
    var sizes = this.data.sizes;
    sizes[sizeIndex].infos.push("");
    this.setData({
      sizes: sizes,
    })
    this.getLastId(sizeIndex)
  },
  //规格名称
  confirmCategoryNameTap: function(event) {
    var dataSet = event.currentTarget.dataset;
    var sizeIndex = dataSet.sizeIndex;
    var sizes = this.data.sizes;
    var value = event.detail.value;
    sizes[sizeIndex].name = value;
    this.setData({
      sizes: sizes
    })
  },

  //规格分类
  confirmCategoryName: function(event) {
    var dataSet = event.currentTarget.dataset;
    var sizeIndex = dataSet.sizeIndex;
    var index = dataSet.index;
    var value = event.detail.value;
    var sizes = this.data.sizes;
    sizes[sizeIndex].infos[index] = value;

    this.setData({
      sizes: sizes,
    })
    this.getLastId(sizeIndex)
  },

  getLastId: function(sizeIndex) {
    var sizes = this.data.sizes;

    var lastId = sizes[sizeIndex].infos.length - 1;
    sizes[sizeIndex].lastId = lastId;

    this.setData({
      sizes: sizes
    })
  },
  //删除规格
  deleteTypesTap: function(event) {
    var dataSet = event.currentTarget.dataset;
    var sizeIndex = dataSet.sizeIndex;
    var sizes = this.data.sizes;
    sizes.splice(sizeIndex, 1);
    this.setData({
      sizes: sizes
    })
  },
  //添加规格
  continueAdd: function() {
    var sizes = this.data.sizes;
    sizes.push({
      name: "",
      infos: [""],
      lastId: 0
    })
    this.setData({
      sizes: sizes
    })
  },
  //保存自定义上商品列表
  saveCustomGoodsTap: function() {
    var that = this
    var size = this.data.sizes;
    var productSpecifications = [];
    if (size == undefined) {

    } else {
      for (var i = 0; i < size.length; i++) {
        var item = size[i];
        var oneGood = {};
        oneGood.specificationName = item.name;
        oneGood.specificationType = {};
        var keyNumber = 0
        for (var j = 0; j < item.infos.length; j++) {
          if (item.infos[j] == "") {
            continue;
          }
          keyNumber++;
          var key = "type" + keyNumber;
          var value = item.infos[j];
          oneGood.specificationType[key] = value;

        }
        productSpecifications.push(oneGood);

      }
    }
    var productSpecificationsJson = JSON.stringify(productSpecifications);
    console.log(productSpecificationsJson)
    if (this.data.name=='') {
      wx.showToast({
        title:'请输入商品名称',
        icon:'none'
      })
    }else{    
      var url = "wx_app/custom_merchandise/saveMerchandiseInfo";
      var data={
        name:this.data.name,
        picUrl: this.data.picUrl,
        productSpecifications: productSpecificationsJson,
        detailPicUrls: this.data.detailPicUrls,
        agentId:this.data.agentId
      }
      console.log(data);
      app.request(url, data, 'GET').then(res => {
        console.log(res)
        if (res.code==1) {
          that.setData({
            success:true
          })
          setTimeout(function(){
            wx.navigateTo({
              url:'../stockManage/stockIndex'
            })
          },1000)
        }else{
          app.errorMsg(res.message)
        }
        // this.saveSuccess(res);
      })
    }
  },
  //上传商品詳情圖片
  bindUploadImg(){  
    let that = this
    wx.chooseImage({
      success: function(res) {
        var tempFilePaths = res.tempFilePaths
        console.log(tempFilePaths)
        let detailPicUrls = []
        for (var i = 0; i < tempFilePaths.length; i++) {
          wx.uploadFile({
            url:appConfig.apiBase+'wx_app/image/upload',
            filePath:tempFilePaths[i],
            name:'image',
            success(res){ 
              var str = res.data.split(',')
              let code = str[0].split(':')[1]
              if (code == 1) {
                let length = str[2].length - 3
                let url = str[2].substring(21,length)
                detailPicUrls.push(url)
                console.log(detailPicUrls)
                that.setData({
                  detailPicUrls
                })
              }else{
                app.errorMsg('上传图片失败')
              }
            }
          })
        }
      }
    })
  },
  //上传商品图片
  bindUploadPic(){
    let that = this
    wx.chooseImage({
      success(res){
        var tempFilePaths = res.tempFilePaths
        wx.uploadFile({
          url:appConfig.apiBase+'wx_app/image/upload',
          filePath:tempFilePaths[0],
          name:'image',
          success(res){
            console.log(res)
            var str = res.data.split(',')
              let code = str[0].split(':')[1]
              if (code == 1) {
                let length = str[2].length - 3
                let url = str[2].substring(21,length)
                that.setData({
                  picUrl:url
                })
              }else{
                app.errorMsg('上传图片失败')
              }
          },
          error(res){
            console.log(res)
          }
        })
      }
    })
  }
})

